package com.example.book.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.author.entity.Authorinformation;
import com.example.author.mapper.AuthorinformationMapper;
import com.example.book.entity.Chapter;
import com.example.book.mapper.ChapterMapper;
import com.example.book.service.ChapterTaskService;
import com.example.book.service.IChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.dtos.ResponseResult;
import com.example.utils.SnowflakeGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 凡哥哥
 * @since 2023-12-01
 */
@Service
@Transactional
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements IChapterService {
    @Autowired
    private AuthorinformationMapper authorinformationMapper;
    @Autowired
    private ChapterMapper chapterMapper;
    @Autowired
    private ChapterTaskService chapterTaskService;
    /**
     * 发布新章节
     *
     * @param chapter
     * @return
     */
    @Override
    public ResponseResult addChapter(Chapter chapter) {
        if (ObjectUtil.isNull(chapter)) {
            return ResponseResult.errorResult(500, "系统错误");
        }
        Long authorId = chapter.getAuthorId();
        if (chapter.getId() != null) {
            chapter.setUpdateTime(LocalDateTime.now());
            chapterMapper.update(chapter);
        } else if (chapter.getId() == null) {

            chapter.setId(SnowflakeGenerator.generate());
            chapter.setCreateTime(LocalDateTime.now());
            if (chapter.getPublishTime() == null){
                chapter.setPublishTime(new Date(System.currentTimeMillis()));
            }
            chapterTaskService.addNewsToTask(chapter.getId(),chapter.getPublishTime());
            chapterMapper.insert(chapter);
        }
        Long totalCount = 0L;
        List<Chapter> chapterList = queryChapterAll(chapter.getBookId());
        for (Chapter chapter1 : chapterList) {
            Long wordCount = Long.parseLong(chapter1.getWordCount());
            totalCount += wordCount;
        }
        Authorinformation authorinformation = Authorinformation.builder()
                .authorId(authorId).follower(0L).totalCount(totalCount).build();
        authorinformationMapper.update(authorinformation);
        return ResponseResult.okResult(authorinformationMapper.findByAuthorId(authorId));

    }

    /**
     * 查看图书全部章节
     *
     * @param bookId
     * @return
     */
    @Override
    public List<Chapter> queryChapterAll(Long bookId) {
        if (bookId == null) {
            return (List<Chapter>) ResponseResult.errorResult(500, "系统错误");
        }
        LambdaQueryWrapper<Chapter> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Chapter::getBookId, bookId)
                .orderByAsc(Chapter::getCreateTime);
        return this.list(lqw);

    }

    /**
     * 根据章节id数据回显
     *
     * @param chapterId
     * @return
     */
    @Override
    public ResponseResult queryChapterById(Long chapterId) {
        return ResponseResult.okResult(this.getById(chapterId));
    }

    /**
     * 删除
     *
     * @param chapterId
     * @return
     */
    @Override
    public ResponseResult delete(Long chapterId) {
        Chapter chapter = chapterMapper.selectById(chapterId);
        Long wordCount = Long.valueOf(chapter.getWordCount());

        Long authorId = chapter.getAuthorId();
        Authorinformation authorinformation = authorinformationMapper.findByAuthorId(authorId);
        Long totalCount = authorinformation.getTotalCount();
        authorinformation.setTotalCount(totalCount-wordCount);
        authorinformationMapper.update(authorinformation);
        this.removeById(chapterId);
        return ResponseResult.okResult(authorinformationMapper.findByAuthorId(authorId));
    }
}
